# _*_ coding: UTF-8 _*_
# @Time     : 2022/5/16 15:51
# @Author   : Li Jie
# @Site     : http://www.hzdlstudy.com/
# @File     : 基于坐标的driver操作.py
# @Software : PyCharm

import unittest
from appium import webdriver
import time

from selenium.webdriver.common.by import By


class CalculatorTest(unittest.TestCase):
    """
    Appium的元素定位方式：
    1.id
    2.xpath
    3.accessbility_id
    """

    def setUp(self) -> None:
        # 手机设置
        desired_caps = {}
        desired_caps['platformName'] = 'Android'  # 系统名称
        desired_caps['platformVersion'] = '7.1.2'  # 系统的版本号
        desired_caps['deviceName'] = 'Android Emulator'  # 设备名称，这里是虚拟机，这个没有严格的规定
        desired_caps['appPackage'] = 'com.netease.cloudmusic'  # APP包名
        desired_caps['appActivity'] = 'com.netease.cloudmusic.activity.LoadingActivity'  # APP入口的activity
        desired_caps['noReset'] = True  # 不重置app的缓存文件
        desired_caps['unicodeKeyboard'] = True  # 设置键盘支持中文输入
        desired_caps['resetKeyboard'] = True  # 重置键盘

        # 连接appium server，告诉appium，代码要操作哪个设备上的哪个APP
        # 启动手机上App
        self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)

    def test(self):
        time.sleep(2)
        self.driver.find_element_by_accessibility_id("搜索").click()  # 点击搜索图标
        time.sleep(1)

        # 清空输入框然后输入
        self.driver.find_element(By.ID, 'com.netease.cloudmusic:id/search_src_text').clear()
        self.driver.find_element(By.ID, 'com.netease.cloudmusic:id/search_src_text').send_keys("雾里")

        time.sleep(2)

        # 基于坐标点击第一个搜索项
        # driver.tap([(x1,y2),(x2,y2)...(x5,y5)],duration)
        self.driver.tap([(100, 151)])

        time.sleep(2)
        # 向上滑动300个像素
        # swipe(self, start_x: int, start_y: int, end_x: int, end_y: int, duration: int = 0)
        self.driver.swipe(270, 480, 270, 180)
        time.sleep(2)

        # 向下滑动300个像素
        self.driver.swipe(270, 480, 270, 780)
        time.sleep(1)

        # 点击结果栏第三项
        self.driver.tap([(150, 300)])
        time.sleep(1)

        #点击播放
        self.driver.tap([(270, 923)])

        time.sleep(120)

    def tearDown(self) -> None:
        self.driver.quit()
